package com.nf.health.app.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hepann on 2016/11/4.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description 汗液动态图, 待优化
 * @email he_pann@163.com
 */

public class SweatShowView extends View {
    private static final int MAX_NUM = 40;//一行数据最大数量
    private int dottedLineColor = 0x44fd9854; //虚线颜色
    private int fullLineColor = 0xfffd9854; //实线颜色
    private int themeColor = 0xff2E86F4; //主题色
    private int width;
    private int height;
    private List<Float> dataList = new ArrayList<>();
    private float mMaxDataReal, mMinDataReal;//所传数据的最值
    private float mMaxData, mMinData;//修正的最值

    public SweatShowView(Context context) {
        this(context, null);
    }

    public SweatShowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SweatShowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.width = getMeasuredWidth();
        this.height = getMeasuredHeight();
        //画背景线
        drawBackground(canvas);
        //画折线
        drawLine(canvas);
        //画文本提示
        drawtext(canvas);
    }


    /**
     * 画背景色
     *
     * @param canvas
     */
    private void drawBackground(Canvas canvas) {
        float widhtInterval = (float) (width / 20.0);
        float heightInterval = (float) (height / 3.0);
        Paint paint = new Paint();
        //设置画直线格式
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        PathEffect effects = new DashPathEffect(new float[]{3, 3, 3, 3}, 1);
        paint.setPathEffect(effects);
        paint.setColor(fullLineColor);
        Path path = new Path();
        path.moveTo(0, 1);
        path.lineTo(width, 1);
        canvas.drawPath(path, paint);
        path.moveTo(1, 0);
        path.lineTo(1, height);
        canvas.drawPath(path, paint);
        path.moveTo(0, height - 1);
        path.lineTo(width, height - 1);
        canvas.drawPath(path, paint);

        paint.setColor(dottedLineColor);
        for (int i = 0; i < 20; i++) {
            float s = widhtInterval * (i + 1);
            canvas.drawLine(s, 0, s, height, paint);
        }
        for (int i = 0; i < 2; i++) {
            float s = heightInterval * (i + 1);
            canvas.drawLine(0, s, width, s, paint);
        }
    }

    private void drawLine(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        paint.setColor(fullLineColor);
        float valueScale = mMaxData - mMinData;//数据范围
        float xScale = width / MAX_NUM * 1.0f;

        if (dataList.size()==1){
            canvas.drawPoint(5, height/2,paint);//画一个点
        }
        //画折线
        float startY;
        float stopY;
        for (int i = 0; i < dataList.size() - 1; i++) {
            startY = height * ((mMaxData - dataList.get(i)) / valueScale);
            stopY = height * ((mMaxData - dataList.get(i + 1)) / valueScale);
            canvas.drawLine(xScale * i, startY, xScale * (i + 1), stopY, paint);
        }
    }


    /**
     * 文本提示,最大最小值
     */
    private void drawtext(Canvas canvas) {
        Paint p = new Paint();
        p.setColor(themeColor);
        p.setAntiAlias(true);
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(1);
        p.setTextSize(18);
//        canvas.drawText("Max:" + mMaxDataReal, 10, 20, p);
//        canvas.drawText("Min:" + mMinDataReal, 10, height - 15, p);
        //最大值
        Rect rect1 = new Rect();
        String max = mMaxDataReal + "";
        p.getTextBounds(max, 0, max.length(), rect1);
        canvas.drawText(max, 10, rect1.height() + 8, p);
        //最小值
        Rect rect2 = new Rect();
        String min = mMinDataReal + "";
        p.getTextBounds(min, 0, min.length(), rect2);
        canvas.drawText(min, 10, height - 8, p);
        //中间值
        Rect rect3 = new Rect();
        float mean = (mMinDataReal + mMaxDataReal) / 2;
        DecimalFormat fnum = new DecimalFormat("##0.00");
        String meanStr = fnum.format(mean);
        p.getTextBounds(meanStr, 0, meanStr.length(), rect3);
        canvas.drawText(meanStr, 10, height / 2 + rect3.height()/2, p);
    }

    public void setDataList(List<Float> data) {
        if (data.size() > MAX_NUM) {
            dataList = data.subList(data.size() - MAX_NUM, data.size());
        } else {
            this.dataList = data;
        }
        float dataValue;
        mMaxData = mMinData = this.dataList.get(0);
        for (int i = 0; i < this.dataList.size(); i++) {
            dataValue = this.dataList.get(i);
            mMaxData = mMaxData < dataValue ? dataValue : mMaxData;
            mMinData = mMinData > dataValue ? dataValue : mMinData;
        }
        mMaxDataReal = mMaxData;
        mMinDataReal = mMinData;
        //对最值做些处理,防止折线画到边界
        float scaleInterval = mMaxData - mMinData;
        if (scaleInterval <= 2) {
            //如果波动范围过小,限制多一些,避免给用户看到大的起伏
            scaleInterval *= 0.1f;
        } else {
            scaleInterval *= 0.1f;
        }
        mMaxData += scaleInterval;
        mMinData -= scaleInterval;
        if (mMinData < 0) {
            mMinData = 0;
        }
        postInvalidate();
    }
}
